package com.xiumu.controller.blog;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiumu.common.core.page.PageQuery;
import com.xiumu.common.core.result.ResultJSON;
import com.xiumu.pojo.blog.dto.BlogPostDTO;
import com.xiumu.pojo.blog.entity.BlogPost;
import com.xiumu.pojo.blog.query.BlogPostQuery;
import com.xiumu.service.blog.BlogPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章信息 Controller
 *
 * @author XiuMu
 * @Date 2023-01-29 09:33:18
 */
@RestController
public class BlogPostController {

    @Autowired
    private BlogPostService blogPostService;

    /**
     * 分页查询
     * 可传递查询参数，按照某些字段分页查询
     *
     * @param pageQuery 分页条件，查询参数
     * @return
     */
    @PostMapping("/page/blogPost")
    public ResultJSON<Page<BlogPost>> page(@RequestBody PageQuery<BlogPostQuery, BlogPost> pageQuery) {
        return ResultJSON.querySuccess(blogPostService.listPage(pageQuery));
    }

    /**
     * 查询全部
     * 可传递查询参数，按照某些字段查询
     *
     * @param blogPost 查询条件
     * @return
     */
    @GetMapping("/blogPost")
    public ResultJSON<List<BlogPost>> list(BlogPostQuery blogPost) {
        List<BlogPost> blogPostList = blogPostService.listByBlogPost(blogPost);
        return ResultJSON.querySuccess(blogPostService.listByBlogPost(blogPost));
    }

    /**
     * 通过 ID 查询
     *
     * @param id ID
     * @return
     */
    @GetMapping("/blogPost/{id}")
    public ResultJSON<BlogPost> find(@PathVariable Long id) {
        return ResultJSON.querySuccess(blogPostService.getById(id));
    }

    /**
     * 创建 blogPost
     *
     * @param blogPostDTO blogPost 信息
     * @return
     */
    @PostMapping("/blogPost")
    public ResultJSON<Boolean> create(@Validated @RequestBody BlogPostDTO blogPostDTO) {
        return ResultJSON.createSuccess(blogPostService.create(blogPostDTO));
    }

    /**
     * 给文章添加标签
     *
     * @param id      文章ID
     * @param tagName 标签名称
     * @return
     */
    @PostMapping("/blogPost/{id}/tag/{tagName}")
    public ResultJSON<Boolean> createTag(@PathVariable Long id, @PathVariable String tagName) {
        return ResultJSON.createSuccess(blogPostService.bindTag(id, tagName));
    }

    /**
     * 给文章删除标签
     *
     * @param id    文章ID
     * @param tagId 标签ID
     * @return
     */
    @DeleteMapping("/blogPost/{id}/tag/{tagId}")
    public ResultJSON<Boolean> deleteTag(@PathVariable Long id, @PathVariable Long tagId) {
        return ResultJSON.createSuccess(blogPostService.deleteTag(id, tagId));
    }

    /**
     * 更新 blogPost
     *
     * @param blogPostDTO blogPost 信息
     * @return
     */
    @PutMapping("/blogPost/{id}")
    public ResultJSON<Boolean> update(@Validated @RequestBody BlogPostDTO blogPostDTO, @PathVariable Long id) {
        return ResultJSON.createSuccess(blogPostService.updateById(blogPostDTO, id));
    }

}
